//
//  GYPersonalViewController.m
//  LockManagement
//
//  Created by apple on 2018/7/24.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYPersonalViewController.h"
#import "GYModfiyPWDView.h"
#import "UIImage+GYColorImage.h"
#import "GYUserInfo.h"
#import "GYNetWorkTools.h"
#import "GYBleOperationManager.h"
#import "GYSpeechSynthesizerTool.h"

@interface GYPersonalViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLable;
@property (weak, nonatomic) IBOutlet UILabel *userNumLable;
@property (weak, nonatomic) IBOutlet UILabel *userNameLable1;
@property (weak, nonatomic) IBOutlet UILabel *userCompanyNameLable;
@property (weak, nonatomic) IBOutlet UIButton *outLoginBtn;

@end

@implementation GYPersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    [self setupUI];
}
- (void)setupUI {
    self.headImageView.layer.cornerRadius = 55;
    self.headImageView.layer.masksToBounds = YES;
    
    self.outLoginBtn.layer.cornerRadius = 5;
    self.outLoginBtn.layer.borderColor = RGBColor(187, 187, 187).CGColor;
    self.outLoginBtn.layer.borderWidth = 1;
    
    //1.设置个人信息
    GYUserInfo *userInfo = [GYUserInfo loadUserInfo];
    UIImage *defaultImage = [UIImage colorImage:CGSizeMake(110, 110) ImageColor:[UIColor whiteColor]];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",userInfo.userPic]] placeholderImage:defaultImage];
    self.userNameLable.text = userInfo.userName;
    self.userNumLable.text = [NSString stringWithFormat:@"%d",userInfo.userID];
    self.userNameLable1.text = userInfo.userName;
    self.userCompanyNameLable.text = userInfo.companyName;
}

- (IBAction)backBtnClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 修改密码

 @param sender <#sender description#>
 */
- (IBAction)modfiyPwdBtnClick:(UIButton *)sender {
    GYModfiyPWDView *modfiyPwdView = [[[NSBundle mainBundle] loadNibNamed:@"GYModfiyPWDView" owner:nil options:nil] firstObject];
    modfiyPwdView.frame = [UIScreen mainScreen].bounds;
    __weak GYModfiyPWDView *weakSelf = modfiyPwdView;
    modfiyPwdView.confirmModfiyPassWordCallBack = ^(NSString *newPassword) {
      //做修改密码的操作
        NSLog(@"执行修改密码的操作...");
        NSString *IP = GETOBJ(@"IP")
        NSString *port = GETOBJ(@"port")
        [GYNetWorkTools requestModflyUserPwdWithURL:[NSString stringWithFormat:@"http://%@:%@/api/password",IP,port] newPassword:newPassword success:^(NSDictionary *obj) {
            //NSLog(@"obj =======%@",obj);
            int state = [[obj objectForKey:@"state"] intValue];
            if (state == 0) {
                [SVProgressHUD showSuccessWithStatus:@"修改密码成功!"];
            }else {
                [SVProgressHUD showErrorWithStatus:@"修改密码失败!"];
            }
        } error:^(NSError *error) {
            NSLog(@"modfiy password error=======%@",error);
        }];
        [weakSelf removeFromSuperview];
    };
    [[UIApplication sharedApplication].keyWindow addSubview:modfiyPwdView];
}


/**
 退出登录点击事件

 @param sender <#sender description#>
 */
- (IBAction)outLoginBtnClick:(UIButton *)sender {
    //退出登录
    NSString *IP = GETOBJ(@"IP")
    NSString *port = GETOBJ(@"port")
    [GYNetWorkTools requestUserLogoutWithURL:[NSString stringWithFormat:@"http://%@:%@/api/logout",IP,port] success:^(NSDictionary *obj) {
        int state = [[obj objectForKey:@"state"] intValue];
        if (state == 0) {
            [SVProgressHUD showSuccessWithStatus:@"退出登录成功!"];
            //1.删掉保存的个人信息
            [GYUserInfo removeUserInfo];
            
            //2.发送通知 要调转到登录界面
            [[NSNotificationCenter defaultCenter]postNotificationName:userlogoutSuccess object:nil];
            
            //3.断开蓝牙
            [[GYBleOperationManager shareOperationManager] disConnectDevice];
            
            //发出语音提示
            GYSpeechSynthesizerTool *tool = [[GYSpeechSynthesizerTool alloc] init];
            [tool textToSpeechConversion:@"蓝牙链接已断开"];
            
        }else {
            [SVProgressHUD showErrorWithStatus:@"退出登录失败,请检查您的网络!"];
        }
    } error:^(NSError *error) {
        NSLog(@"login out  errro======%@",error);
    }];
}

@end
